package com.vanysoft.framework.config.mvc;

import com.vanysoft.framework.redis.RedisService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * MVC配置
 *
 * @author qi_ma
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
    private final RedisService redisService;

    public WebConfiguration(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(userInfoArgumentsResolver());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/");
    }

    @Bean
    UserInfoArgumentsResolver userInfoArgumentsResolver() {
        return new UserInfoArgumentsResolver(redisService);
    }

    /**
     * 注册拦截器
     * InterceptorRegistration ir = registry.addInterceptor(new InitInterceptor());
     * 配置拦截的路径
     * ir.addPathPatterns("/**.*");
     * 配置不拦截的路径
     * ir.excludePathPatterns("/**.html");
     * registry.addInterceptor(new InitInterceptor()).addPathPatterns("/index");
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    }
}